package com.lyc.campuslife.commonService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import static com.lyc.campuslife.utils.RedisConstants.LOGIN_CODE_TTL;

@Slf4j
@Service
public class EmailService {
    private String MAIL_SENDER;
    private final JavaMailSender javaMailSender;


    public EmailService(@Autowired JavaMailSender javaMailSender,@Value("${spring.mail.username}") String sender) {
        this.javaMailSender = javaMailSender;
        this.MAIL_SENDER = sender;
    }
    public boolean sendLoginCode(String to,String code){
        String subject = "羽穿科技验证码";
        String content = "尊敬的用户，您的注册登录验证码为： "+code+" 有效期： "+LOGIN_CODE_TTL+" 分钟";
        return sendSimpleEmail(to,subject,content);
    }
    public boolean sendSimpleEmail(String to, String subject, String content) {
        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            //发送者
            mailMessage.setFrom(MAIL_SENDER);
            //接收者
            mailMessage.setTo(to);
            //邮件标题
            mailMessage.setSubject(subject);
            //邮件内容
            mailMessage.setText(content);
            //发送邮箱
            javaMailSender.send(mailMessage);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage());
            return false;
        }
    }
}
